package com.myzk.example.utils.common;

import java.io.*;

/**
 * @author mayong
 * @date 2020年04月06日22:07:15
 */
public class IOUtils {
    public static boolean closeQuietly(Closeable closeables){
        boolean rst = false;
        if(closeables!=null){
            try {
                closeables.close();
                rst = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return rst;
    }

    public static String copyToString(InputStream input){
        return copyToString(input, "UTF-8");
    }

    private static String copyToString(InputStream input, String charsetName) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try{
            byte[] buffer = new byte[4096];
            int bytesRead = -1 ;
            while((bytesRead=input.read(buffer))!=-1){
                baos.write(buffer,0,bytesRead);
            }
            baos.flush();
            return baos.toString(charsetName);
        }catch (Exception e){
            return null;
        }finally {
            closeQuietly(baos);
            closeQuietly(input);
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        String string = copyToString(new FileInputStream("E:\\docs\\markdownDoc\\api.md"));
        System.out.println(string);
    }
}
